/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Productos;

import Utilidades.utils;
import de.javasoft.plaf.synthetica.SyntheticaWhiteVisionLookAndFeel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author alrojas
 */
public class IListar extends JFrame{
    
    JTable porVencerTab;
    JTable porAcabarTab;
    
    boolean hayObservados;

    public IListar() throws HeadlessException {
        
        super("Productos con observaciones");
        setLayout(new BorderLayout());
        
        hayObservados = false;
        
        inicializarComponentes();
        inicializarListeners();
        
        this.pack();
        //this.setVisible(true);
        this.setLocationRelativeTo(null);
        
        
        
    }
    
    public void inicializarComponentes(){
        
        Font fNegrita = new Font("Arial",1,17);
        Font fRegular = new Font("Arial",0,17);
        
        Vector < String > columnNames1 = new Vector< String >(); 
        columnNames1.add("Código"); 
        columnNames1.add("N. Comercial"); 
        columnNames1.add("N. Genérico"); 
        columnNames1.add("Ex. Mínimo");
        columnNames1.add("Ex. Actual"); 
        
        Vector < Vector < String > > productos1 = null;
        try {
            productos1 = CProducto.damePorAcabar();
            if(productos1.size() > 0) this.hayObservados = true;
        } catch (SQLException ex) {
            Logger.getLogger(IListar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IListar.class.getName()).log(Level.SEVERE, null, ex);
        }

        DefaultTableModel modelo1 = new DefaultTableModel(productos1,columnNames1){

            @Override
            public boolean isCellEditable (int row, int column){
                return false;
            }
        };
        
        DefaultTableCellRenderer renderer1 = new DefaultTableCellRenderer(){
                
                public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column){
                    
                    super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);

                    double porcentaje = Double.valueOf(table.getValueAt(row,4).toString()) * 100.0 / Double.valueOf(table.getValueAt(row,3).toString());
                
                    if(porcentaje < 50){
                        this.setBackground(Color.RED);
                        this.setForeground(Color.YELLOW);
                        this.setOpaque(true);
                    }
                    else if(porcentaje < 100){
                        this.setBackground(Color.YELLOW);
                        this.setForeground(Color.RED);
                        this.setOpaque(true);
                    }
                    else{
                        this.setBackground(Color.WHITE);
                        this.setForeground(Color.BLACK);
                        this.setOpaque(true);
                    }

                 return this;
                }
        };
    
        renderer1.setHorizontalAlignment(SwingConstants.CENTER);
                
        porAcabarTab = new JTable(modelo1);
        porAcabarTab.getTableHeader().setFont(fNegrita);
        porAcabarTab.setFont(fRegular);
        porAcabarTab.setRowHeight(30);
        porAcabarTab.getColumnModel().getColumn(0).setCellRenderer(renderer1);
        porAcabarTab.getColumnModel().getColumn(1).setCellRenderer(renderer1);
        porAcabarTab.getColumnModel().getColumn(2).setCellRenderer(renderer1);
        porAcabarTab.getColumnModel().getColumn(3).setCellRenderer(renderer1);
        porAcabarTab.getColumnModel().getColumn(4).setCellRenderer(renderer1); 
        
        JScrollPane barras1 = new JScrollPane(porAcabarTab); 
        barras1.setPreferredSize(new Dimension(600,200));
        barras1.setBorder(new TitledBorder("Por Acabarse!"));
        add(barras1,BorderLayout.NORTH);
        
        Vector < String > columnNames2 = new Vector< String >(); 
        columnNames2.add("Código"); 
        columnNames2.add("N. Comercial"); 
        columnNames2.add("N. Genérico"); 
        columnNames2.add("F. Vencimiento"); 
        columnNames2.add("Dias Vencidos"); 
        
        Vector < Vector < String > > productos2 = null;
        try {
            productos2 = CProducto.damePorVencer();
            if(productos2.size() > 0) this.hayObservados = true;
        } catch (SQLException ex) {
            Logger.getLogger(IListar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IListar.class.getName()).log(Level.SEVERE, null, ex);
        }

        DefaultTableModel modelo2 = new DefaultTableModel(productos2,columnNames2){

            @Override
            public boolean isCellEditable (int row, int column){
                return false;
            }
        };
        
        DefaultTableCellRenderer renderer2 = new DefaultTableCellRenderer(){
                
            public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column){
            super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);

                if(Integer.parseInt(table.getValueAt(row,4).toString()) < 15){
                    this.setBackground(Color.RED);
                    this.setForeground(Color.YELLOW);
                    this.setOpaque(true);
                }
                else if(Integer.parseInt(table.getValueAt(row,4).toString()) < 30){
                    this.setBackground(Color.YELLOW);
                    this.setForeground(Color.RED);
                    this.setOpaque(true);
                }
                else{
                    this.setBackground(Color.WHITE);
                    this.setForeground(Color.BLACK);
                    this.setOpaque(true);
                }

             return this;
            }
        };
        renderer2.setHorizontalAlignment(SwingConstants.CENTER);
        
        porVencerTab = new JTable(modelo2);
        porVencerTab.getTableHeader().setFont(fNegrita);
        porVencerTab.setFont(fRegular);
        porVencerTab.setRowHeight(30);
        porVencerTab.getColumnModel().getColumn(0).setCellRenderer(renderer2);
        porVencerTab.getColumnModel().getColumn(1).setCellRenderer(renderer2);
        porVencerTab.getColumnModel().getColumn(2).setCellRenderer(renderer2);
        porVencerTab.getColumnModel().getColumn(3).setCellRenderer(renderer2);
        porVencerTab.getColumnModel().getColumn(4).setCellRenderer(renderer2); 
        
        JScrollPane barras2 = new JScrollPane(porVencerTab);
        barras2.setPreferredSize(new Dimension(600,200));
        barras2.setBorder(new TitledBorder("Por Vencerse!"));
        add(barras2,BorderLayout.SOUTH);
        
    }
    
    public void inicializarListeners(){
         
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cerrar"); 
        getRootPane().getActionMap().put("Cerrar", new AbstractAction(){
            public void actionPerformed(ActionEvent e)
            {
                setVisible(false);
            }
        });        
    }
    

    
    public boolean hayObservados(){
        return this.hayObservados;
    }
    
    public void mostrar(){
        this.setVisible(true);
    }
    
        public static void main(String args[]){
     
          try{
              UIManager.setLookAndFeel(new SyntheticaWhiteVisionLookAndFeel());
        }catch (Exception e){ 
        }
        //IListar i = new IListar();
        
        final JFrame g = new JFrame("Ventanita");
        g.setPreferredSize(new Dimension(400,400));
        g.setSize(new Dimension(400,400));
        g.setVisible(true);
        
        JButton a = new JButton("Mostrar");
        a.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
             g.setVisible(true);
                }
            });
        JButton b = new JButton("Ocultar");
        b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    g.setVisible(false);
                }
            });
        
        JFrame f = new JFrame("Botoncitos");
        f.setLayout(new FlowLayout());
        f.add(a);
        f.add(b);
        f.pack();
        f.setVisible(true);
        

          
     }
    
}
